package training;

import java.util.*;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_9_7_Training {
    //有序矩阵中第 K 小的元素
    public int kthSmallest(int[][] matrix, int k) {
        int ans = 0;
        int x = 0;
        int y = 0;
        for (int m = 0; m < k; m++) {
            ans = Integer.MAX_VALUE;
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[0].length; j++) {
                    if(matrix[i][j] == Integer.MAX_VALUE){
                        continue;
                    }
                    if(matrix[i][j] > ans){
                        break;
                    }
                    if(ans >= matrix[i][j]){
                        ans = matrix[i][j];
                        x = i;
                        y = j;
                    }
                }
            }
            matrix[x][y] = Integer.MAX_VALUE;
        }
        return ans;
    }

    //高访问员工
    public List<String> findHighAccessEmployees(List<List<String>> access_times) {
        Map<String,List<Integer>> map = new HashMap<>();
        List<String> res = new ArrayList<>();
        for(List<String> list:access_times){
            map.putIfAbsent(list.get(0),new ArrayList<Integer>());
            List<Integer> time =  map.get(list.get(0));
            time.add(60*Integer.parseInt(list.get(1).substring(0,2))+Integer.parseInt(list.get(1).substring(2,4)));
            map.put(list.get(0),time);
        }
        for(Map.Entry<String,List<Integer>> entry:map.entrySet()){
            List<Integer> list = entry.getValue();
            if(list.size()<3) {
                continue;
            }
            Collections.sort(list);
            for(int i=0;i<list.size()-2;i++){
                if(list.get(i+2)-list.get(i)<60) {
                    res.add(entry.getKey());
                    break;
                }
            }
        }
        return res;
    }
}
